﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
#if MONODROID
using System.Runtime.Serialization.Json;
#else
using System.Web.Script.Serialization;
#endif

namespace Devsoft.Utilities.Extentions
{
    public static class JsonSerializationHelper
    {
        public static T FromJsonFile<T>(string fn)
        {
            var serializer = new DataContractJsonSerializer(typeof(T));
            using (var ms = new FileStream(fn, FileMode.Open))
            {
                return (T)serializer.ReadObject(ms);
            }
        }

        public static void ToJsonFile<T>(this T source, string fn)
        {
            var serializer = new DataContractJsonSerializer(typeof(T));
            using (var ms = new FileStream(fn, FileMode.Create))
            {
                serializer.WriteObject(ms, source);
            }
        }

        public static T DeserializeJsonAs<T>(this string json)
        {

            var serializer = new DataContractJsonSerializer(typeof(T));
            using (var ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
            {
                return (T)serializer.ReadObject(ms);
            }
        }

        public static string ToJsonString<T>(this T source)
        {
#if MONODROID   
            throw new NotImplementedException();
#else
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(source);
#endif
        }

        public static void SerializeJsonToFile<K,V>(this IDictionary<K,V> source,string fn)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IDictionary<K, V>));
            using (FileStream ms = new FileStream(fn,FileMode.Create))
            {
                serializer.WriteObject(ms, source);
            }
        }

        public static void LoadFromJsonFile<K, V>(this IDictionary<K, V> source, string fn)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IDictionary<K, V>));
            using (FileStream ms = new FileStream(fn,FileMode.Open))
            {
                Dictionary<K, V> tmp = (Dictionary<K, V>)serializer.ReadObject(ms);
                source.Clear();
                foreach (KeyValuePair<K, V> valuePair in tmp)
                {
                    source.Add(valuePair.Key,valuePair.Value);
                }
            }
        }

        public static void LoadFromJson<K, V>(this IDictionary<K, V> source, string json)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IDictionary<K, V>));
            using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
            {
                Dictionary<K, V> tmp = (Dictionary<K, V>)serializer.ReadObject(ms);
                source.Clear();
                foreach (KeyValuePair<K, V> valuePair in tmp)
                {
                    source.Add(valuePair.Key, valuePair.Value);
                }
            }
        }

    }
}
